<?php
	if (!isset($_SESSION)) {
		 session_start();
	}
	// Clear previous messages.
	if(isset($_SESSION['error'])) unset($_SESSION['error']);
	if(isset($_SESSION['success'])) unset($_SESSION['success']);
	
	// Get values from the form
	$startDate = $_POST['startDate'];
	$endDate = $_POST['endDate'];
	
	// Give date format for the date
	$dateFormat = '/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/';
	
	// Check for validation
	if(empty($startDate) || empty($endDate)){
		$_SESSION['error'] = "Pleace complete the mandatory fields";
		$_SESSION['page'] = "viewProfit.php";
	}else if($startDate > $endDate){
		$_SESSION['error'] = "End date should be larger than the start date.";
		$_SESSION['page'] = "viewProfit.php";
	}else if(preg_match($dateFormat, $startDate)==true && preg_match($dateFormat, $endDate)==true){
		include("../dbconnection.php");
		if($result = mysql_query("SELECT orderId, orderDate, userName, totalPrice FROM orders WHERE orderDate <= '$endDate' AND orderDate >= '$startDate'")){
			$num = mysql_num_rows($result);
			if($num == 0)
				$_SESSION['error'] = "No orders have happened within this period";
			else{
				$output[] = '<table id="profitTable" border="1" cellpadding="1" cellspacing="0" style="width:100%">';
				$output[] = '<thead>';
				$output[] = '<tr>';
				$output[] = '<th>Order No</th>';
				$output[] = '<th>Order Date</th>';
				$output[] = '<th>Placed By</th>';
				$output[] = '<th>Order Income</th>';
				$output[] = '<th>Order Expenditure</th>';
				$output[] = '<th>Order Profit</th>';
				$output[] = '</tr>';
				$output[] = '</thead>';
				$output[] = '<tbody>';
					
				$i=0;
				$totalIncome = 0;
				$totalExpenditure = 0;
				$totalProfit = 0;
				while ($i < $num) {
					$orderId=mysql_result($result,$i,"orderId");
					$orderDate=mysql_result($result,$i,"orderDate");
					$userName=mysql_result($result,$i,"userName");
					$orderIncome=mysql_result($result,$i,"totalPrice");
					
					$sql = "SELECT order_items.quantity, stock.boughtPrice
							FROM  `order_items` 
							INNER JOIN  `stock` ON order_items.itemCode = stock.itemCode
							AND order_items.batchId = stock.batchId
							WHERE orderId = $orderId";
					if($result2 = mysql_query($sql)){
						$num2 = mysql_num_rows($result2);
						$j = 0;
						$orderExpenditure = 0;
						while ($j < $num2){
							$itemQuantity=mysql_result($result2,$j,"order_items.quantity");
							$itemBoughtPrice=mysql_result($result2,$j,"stock.boughtPrice");
							$orderExpenditure += ($itemQuantity * $itemBoughtPrice);
							$j++;
						}
					}
					$orderProfit = $orderIncome - $orderExpenditure;
					$output[] = '<tr>';
					$output[] = '<td>'.$orderId.'</td>';
					$output[] = '<td>'.$orderDate.'</td>';
					$output[] = '<td>'.$userName.'</td>';
					$output[] = '<td>'.$orderIncome.'</td>';
					$output[] = '<td>'.$orderExpenditure.'</td>';
					$output[] = '<td>'.$orderProfit.'</td>';
					$output[] = '</tr>';
					$totalIncome += $orderIncome;
					$totalExpenditure += $orderExpenditure;
					$totalProfit += $orderProfit;
					$i++;
				}
				$output[] = '</table>';
				$_SESSION['profit'] = join('',$output);
				
				$totalProfit = $totalIncome - $totalExpenditure;
				$_SESSION['totalExpenditure'] = $totalExpenditure;
				$_SESSION['totalIncome'] = $totalIncome;
				$_SESSION['totalProfit'] = $totalProfit;
			}
		}else{
			$_SESSION['error'] = "Error occured ";
			$_SESSION['page'] = "viewProfit.php";
		}
	}else{
		$_SESSION['error'] = "Pleace enter the correct date format";
		$_SESSION['page'] = "addStock.php";
	}
	
	
	$_SESSION['page'] = "viewProfit.php";
	header('Location: ../../');	
	
?>	
